package com.item.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import sun.misc.BASE64Encoder;

import com.base.support.PropertySupport;
import com.item.scraputils.fetcher.BaseFetcher;

/**
 * 手机短信继承，发送短信验证码和错误预警
 * @author yjh
 *
 */
public class ShortMessageSender {
	//短信格式
	private static String MESSEAGE_FORMAT = PropertySupport.getProperty("service.register_format");
	private static String MESSEAGE_ERROR_FORMAT = PropertySupport.getProperty("service.error_format");
	private static String SEND_SERVICE_URL = "https://sms-api.luosimao.com/v1/send.json";
	private static String ACCOUNT =  "";
	static {
		try {
			ACCOUNT = "Basic " + new BASE64Encoder().encode(PropertySupport.getProperty("service.account").getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 想指定号码发送短信验证码，成功返回验证码，否则返回null
	 * @param phone
	 * @return 短信验证码，非null表示成功发送
	 */
	public static String send(String phone, String verify) {
		String result = null;
		//获取随机验证码
		if(StringUtils.isBlank(verify))
			verify = verifyGenerator();
		//读取
		String messageTemp = String.format(MESSEAGE_FORMAT, verify);
		//请求第三方服务
		Map<String,String> headers = new HashMap<String,String>();
		headers.put("Authorization",ACCOUNT);
		BaseFetcher fetcher = new BaseFetcher();
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("mobile", phone));
		nvps.add(new BasicNameValuePair("message", messageTemp));
		String response = fetcher.getPostContent(SEND_SERVICE_URL, nvps, "utf8", headers);
		//验证是否发送成功
		if(StringUtils.isNotBlank(response) && response.contains("\"msg\":\"ok\"")) {
			result = verify;
		}
		fetcher.close();
		return result;
	}
	
	/**
	 * 产生6位随机验证码
	 * @return
	 */
	public static String verifyGenerator() {		
		return (int) ((Math.random()) * (Math.pow(10, 4) - 1000 - 1) + 1000) + "";
	}
	
	
	
	/**
	 * 想指定号码发送短信验证码，成功返回验证码，否则返回null
	 * @param phone
	 * @return 短信验证码，非null表示成功发送
	 */
	public static void sendUtil(String phone, String message) {
		//请求第三方服务
		Map<String,String> headers = new HashMap<String,String>();
		headers.put("Authorization",ACCOUNT);
		BaseFetcher fetcher = new BaseFetcher();
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("mobile", phone));
		nvps.add(new BasicNameValuePair("message", message));
		String response = fetcher.getPostContent(SEND_SERVICE_URL, nvps, "utf8", headers);
		System.out.println(response);
		//验证是否发送成功
//		if(response.contains("\"msg\":\"ok\"")) {
//			result = verify;
//		}
		fetcher.close();
	}
	
	public static void main(String[] args) {
		ShortMessageSender.sendUtil("15996282157", "帖子加一【掌上大学】");
	}
	
}



/*
 * class MyThread implements Runnable {
    public void run() {
    	ShortMessageSender.send("18120156552");
    	ShortMessageSender.send("15062218862");
    	ShortMessageSender.send("15298387125");
    	ShortMessageSender.send("13951894544");
    	ShortMessageSender.send("18651679797");
    	ShortMessageSender.send("15996235494");
    }
}

 */